package com.controller;

import com.config.api.CommonResult;
import com.entity.table.DrinkTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Drink;
import com.service.DrinkService;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author admin
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/admin/drink")
public class DrinkController {

    @Autowired
    private DrinkService drinkService;

    /**
     * 添加。
     *
     * @param drink 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Drink drink) {
//        return drinkService.save(drink);
//    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return drinkService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param drink 
     * @return {@code true} 更新成功，{@code false} 更新失败
//     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Drink drink) {
//        return drinkService.updateById(drink);
//    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Drink> list() {
//        return drinkService.list();
//    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public Drink getInfo(@PathVariable Serializable id) {
        return drinkService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Drink> page(Page<Drink> page) {
        return drinkService.page(page);
    }

    /**
     * 添加商品类型表。
     *
     * @param drink 商品类型表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Drink drink) {
        try {
            boolean r = drinkService.save(drink);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String  msg = drink.getId()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        Drink drink = drinkService.getById(id);
        model.addAttribute("drink",drink);
        return "back/drink-edit";
    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Drink bType){
        try {
            boolean r = drinkService.updateById(bType);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String  msg = bType.getId()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "/list")
    public String list(ModelMap model,
                           @RequestParam(value = "drink",defaultValue = "") String drinkid,
                           @RequestParam(value = "p",defaultValue = "1") int p,
                           @RequestParam(value = "pageSize",defaultValue = "10")int paqeSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(DrinkTableDef.DRINK.ID.like(drinkid));
        Page<Drink> page = new Page<>();
        page.setPageSize(paqeSize);
        page.setPageNumber(p);
        Page<Drink> pageInfo = drinkService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/drink-list";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = drinkService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    @GetMapping(value = "/addView")
    public String addUserView() { return "back/drink-add"; }
}
